﻿<%@ WebHandler Language="C#" Class="HandlerUserPublishedRecord" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;
public class HandlerUserPublishedRecord : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string strconn = LinkToDatabase.strConn;/*App_Code->UserClass.cs*/
        string strcmd = "";
        string type = context.Request.Form["type"];
        int user_id = Convert.ToInt32(context.Request.Cookies["u_id"].Value);//*-***************記得要改Convert.ToInt32(context.Request.Cookies["u_id"].Value)
        using (SqlConnection conn = new SqlConnection(strconn))
        {
            switch (type)
            {
                //1=======================================================================
                case "1":
                    strcmd = @"SELECT [unboxing_article_id] as uid
                              ,[unboxing_article_title] as title
                              ,CONVERT(varchar, [unboxing_article_publish_date], 120 )  as date   
                              FROM [UBOX].[dbo].[unboxing_article]
                              where [user_id]=@Id
							  order by [unboxing_article_publish_date] Desc
                              OFFSET @Num ROWS FETCH NEXT @Pagination ROWS ONLY 
                            ";
                    break;
                //2=======================================================================
                case "2":
                    strcmd = @"select count(*) as num
                                     FROM [UBOX].[dbo].[unboxing_article] 
                                     where [user_id]=@Id
                            ";
                    break;

            }
            using (SqlDataAdapter da = new SqlDataAdapter(strcmd, conn))
            {
                SqlParameter Result;
                Result = new SqlParameter("@Result", SqlDbType.Bit);
                Result.Direction = ParameterDirection.Output;
                switch (type)
                {
                    //1=======================================================================
                    case "1":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Id", user_id);
                        da.SelectCommand.Parameters.AddWithValue("@Num", (Convert.ToInt32(context.Request.Form["PageNum"]) - 1) * Convert.ToInt32(context.Request.Form["Pagination"]));
                        da.SelectCommand.Parameters.AddWithValue("@Pagination", Convert.ToInt32(context.Request.Form["Pagination"]));
                        
                        break;
                    //2=======================================================================
                    case "2":
                        da.SelectCommand.Parameters.AddWithValue("@Id", user_id);
                        break;

                }
                DataSet ds = new DataSet();
                switch (type)
                {
                    //12=======================================================================
                    case "1":
                    case "2":
                        da.Fill(ds, "data");
                        context.Response.ContentType = "application/json";
                        context.Response.Write(JsonConvert.SerializeObject(ds));
                        break;
                }
                conn.Close();
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}